import { VersionPage } from './../version/version';
import { ScanPage } from './../scan/scan';
import { SettingProvider } from './../../providers/setting/setting';
import { RestProvider } from './../../providers/rest/rest';
import { LoadingController } from 'ionic-angular/components/loading/loading-controller';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { ModalController } from 'ionic-angular/components/modal/modal-controller';
import { LoginPage } from '../login/login';
import { Storage } from '@ionic/storage'
import { BaseUI } from '../../common/baseui';
import { UserPage } from '../user/user';
import { UserdatalistPage } from '../userdatalist/userdatalist';


/**
 * Generated class for the MorePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */


@Component({
  selector: 'page-more',
  templateUrl: 'more.html',
})
export class MorePage extends BaseUI {

  public notLogin: boolean = true
  public logined: boolean = false
  headface: string
  userinfo: string[]
  selectedTheme: string

  constructor(public navCtrl: NavController, 
    public navParams: NavParams,
    public settings: SettingProvider,
    public modalCtrl: ModalController,
    public loadCtrl: LoadingController,
    public rest: RestProvider,
    public storage: Storage) {
      super()
    this.settings.getActiveTheme().subscribe(val => {
      this.selectedTheme = val
    })
  }

  showModal() {
    let modal = this.modalCtrl.create(LoginPage)
    // 关闭之后的回调，modal关闭之后不会触发父页面的刷新
    modal.onDidDismiss(() => {
      this.loadUserPage()
    })
    modal.present()
  }

  ionViewDidLoad(){
   this.loadUserPage()
  }

  loadUserPage() {
    this.storage.get('userId').then((val)=> {
      if(val!=null) {
        // 加载用户数据
        let loading = super.showLoading(this.loadCtrl, "加载中...")
        this.rest.getUserInfo(val)
        .subscribe(
          userinfo => {
            this.userinfo = userinfo
            this.headface = userinfo["UserHeadface"] + "?" + (new Date()).valueOf()
            this.notLogin = false
            this.logined = true
            loading.dismiss()
          }
        )
      } else {
        this.notLogin = true
        this.logined = false
      }
    })
  }

  gotoDataList(type) {
    this.navCtrl.push(UserdatalistPage, {"dataType": type})
  }
  
  gotoUserPage() {
    this.navCtrl.push(UserPage)
  }

  /**
   * 跳转到扫描二维码的页面，加上animate的参数是为了
   * 相机能够在整个屏幕中显示，如果不加，相机就出不来
   * animate 的参数默认值喂true
   * @memberof MorePage
   */
  gotoScanQRCode() {
    this.navCtrl.push(ScanPage, null, {"animate":false})
  }

  gotoVersions() {
    this.navCtrl.push(VersionPage)    
  }

  toggleChangeTheme() {
    if (this.selectedTheme === 'dark-theme') {
      this.settings.setActiveTheme('light-theme')
    } else {
      this.settings.setActiveTheme('dark-theme')      
    }
  }
}


